//
// Created by chizuru on 2025/9/2.
//

#pragma once
#include "IDeviceIO.h"
#include "RetryPolicy.h"
#include "CLBProtocolCodec.h"
#include "OperationResult.h"
#include "IResponseTerminator.h"
#include <mutex>
#include <vector>
#include <iostream>
#include <chrono>
#include <thread>
#include <any>

//using namespace std::chrono_literals;
class DeviceComm {
public:
    DeviceComm(IDeviceIO& device);

    OperationResult configureSerial(std::any baud, std::any dataBits, std::any parity, std::any stopBits, std::any flowctrl);

    bool flush() ;

    // send frame, read response until terminator says complete, return OperationResult and populate outResp
    OperationResult sendFrameAndReadResponse(const std::vector<uint8_t>& frame,
                                             const IResponseTerminator& terminator,
                                             std::vector<uint8_t>& outResp,
                                             std::chrono::milliseconds duration,
                                             std::chrono::milliseconds timeoutMs,
                                             std::chrono::milliseconds interReadDelay);

private:
    IDeviceIO& device;

    std::mutex mtx;

};

